package com.douxg.fastCoding.web.util;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;

/**
 * @Description 本类主要处理数据库数据的转换比如list转map，map转list
 * @Auther douxg
 * @Date 2021/4/26
 */
public class DataConverUtil {
    /**
     * 将listmap数据修改为map
     *
     * @param list
     * @param keyStr
     * @param valueStr
     * @return
     */
    public static Map<String, Object> listToMap(List list, String keyStr, String valueStr) {
        Map<String, Object> map = new HashMap<>();
        if (list == null || list.size() == 0) {
            return map;
        }
        for (Object obj : list) {
            String key = null;
            Object value = null;
            if (obj instanceof Map) {
                key = Optional.ofNullable((Map) obj).map(map1 -> map1.get(keyStr)).map(o -> o.toString()).orElse(null);
                value = Optional.ofNullable((Map) obj).map(map1 -> map1.get(valueStr)).map(o -> o.toString()).orElse(null);
            } else {
                Object o = getClassFieldValue(obj, keyStr);
                if (o != null) {
                    key = o.toString();
                }
                value = getClassFieldValue(obj, valueStr);
            }
            map.put(key, value);
        }
        return map;
    }

    /**
     * 将listmap数据修改为map
     *
     * @param list
     * @param kFun
     * @param vFun
     * @return
     */
    public static Map<String, Object> listToMap(List<Map<String, Object>> list, Function<Map<String, Object>, String> kFun, Function<Map<String, Object>, Object> vFun) {
        Map<String, Object> map = new HashMap<>();
        if (list == null || list.size() == 0) {
            return map;
        }
        for (Map<String, Object> stringObjectMap : list) {
            String key = kFun.apply(stringObjectMap);
            Object value = vFun.apply(stringObjectMap);
            map.put(key, value);
        }
        return map;
    }

    /**
     * 将listmap数据修改为map
     *
     * @param list
     * @param keyStr
     * @return
     */
    public static <T> Map<String, T> listToMap(List<T> list, String keyStr) {
        Map<String, T> map = new HashMap<>();
        if (list == null || list.size() == 0) {
            return map;
        }
        for (T obj : list) {
            String key = null;
            if (obj instanceof Map) {
                key = Optional.ofNullable((Map) obj).map(map1 -> map1.get(keyStr)).map(o -> o.toString()).orElse(null);
            } else {
                Object o = getClassFieldValue(obj, keyStr);
                if (o != null) {
                    key = o.toString();
                }
            }
            map.put(key, obj);
        }
        return map;
    }

    /**
     * 将数据中的指定值替换为字典中的值
     *
     * @param dictMap
     * @param list
     * @param key
     * @param newKey
     * @param defValue 没有对应值之后的默认值
     */
    public static void dictReplace(Map<String, Object> dictMap, List<Map<String, Object>> list, String key, String newKey, Object defValue) {
        if (dictMap == null) {
            return;
        }
        for (Map<String, Object> map : list) {
            if (map == null) {
                continue;
            }
            String v = Optional.ofNullable(map.get(key)).map(o -> o.toString()).orElse(null);
            Object o = dictMap.get(v);
            if (o == null) {
                o = defValue;
            }
            map.put(newKey, o);
        }
    }

    /**
     * 将数据中的指定值替换为字典中的值,如果没有匹配到就不替换
     *
     * @param dictMap
     * @param list
     * @param key
     * @param newKey
     * @param defValue 没有对应值之后的默认值
     */
    public static void dictReplaceNoEmpty(Map<String, Object> dictMap, List<Map<String, Object>> list, String key, String newKey, Object defValue) {
        if (dictMap == null) {
            return;
        }
        for (Map<String, Object> map : list) {
            if (map == null) {
                continue;
            }
            String v = Optional.ofNullable(map.get(key)).map(o -> o.toString()).orElse(null);
            Object o = dictMap.get(v);
            if (o == null) {
                o = defValue;
            }
            if (o != null) {
                map.put(newKey, o);
            }
        }
    }

    /**
     * 将数据中的指定值替换为字典中的值,如果没有匹配到就舍弃
     *
     * @param dictMap
     * @param list
     * @param key
     * @param newKey
     * @param defValue 没有对应值之后的默认值
     */
    public static void dictReplaceRemoveEmpty(Map<String, Object> dictMap, List<Map<String, Object>> list, String key, String newKey, Object defValue) {
        if (dictMap == null) {
            return;
        }
        list.removeIf(map -> {
            String v = Optional.ofNullable(map).map(map1 -> map1.get(key)).map(o -> o.toString()).orElse(null);
            Object o = dictMap.get(v);
            if (o == null) {
                o = defValue;
            }
            if (o != null) {
                map.put(newKey, o);
                return false;
            }
            return true;
        });
    }

    public static Object getClassFieldValue(Object obj, String keyStr) {
        Object key = null;
        try {
            Field declaredField = obj.getClass().getDeclaredField(keyStr);
            declaredField.setAccessible(true);
            key = declaredField.get(obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return key;
    }
}
